DragTool = function(data){
	this.init(data);
}
DragTool.prototype = {
		siblingDom:null,
		currentDom:null,
		parentId:null,
		parentDom:null,
		selectedColor:'#E0ECFF',
		dragCurDom:null,
		className:null,
		exceptClass:null,
		parentWidth:null,
		parentHeight:null,
		parentLeft:null,
		parentTop:null,
		src:0,
		target:0,
		init:function(data){
			if(data.id && data.className){
				this.parentId = data.id;
				this.className = data.className;
			} else {
				$.messager.alert('拖拽工具提示', '初始化失败', 'warning');
				return;
			}
			this.parentDom = $('#' + this.parentId);
			this.processDom();
		},
		drag:function(){
			var impl = this;
			$('.' + this.className).draggable({
				onStartDrag : function(e){
					impl.setSrc(e);
				},
				onDrag : function(e){
					var d = e.data;
					var parLeft = $(d.parent).offset().left;
					var parTop = $(d.parent).offset().top;
					if (d.left < parLeft) {
						d.left = parLeft;
					}
					if (d.top < parTop) {
						d.top = parTop;
					}
					if (d.left + $(d.target).outerWidth() > ($(d.parent).width() + parLeft)) {
						d.left = $(d.parent).width() + parLeft - $(d.target).outerWidth();
					}
					if (d.top + $(d.target).outerHeight() > ($(d.parent).height() + parTop)) {
						d.top = $(d.parent).height() + parTop - $(d.target).outerHeight();
					}
				},
				proxy : 'clone',
				revert : true,
				deltaX : 0,
				deltaY : 0
			}).droppable({
				onDragOver : function(e, source){
					var eDom = e.target;
					$('.' + impl.className).css('background-color','#ffffff');
					$(eDom).css('background-color','#00ff00');
				},
				onDragEnter:function(e,source){
					$(source).draggable('options').cursor='auto';
				},
				onDragLeave:function(e,source){
					$(source).draggable('options').cursor='not-allowed';
				},
				onDrop:function(e,source){
					impl.target = $('.' + impl.className).index(e.target);
					impl.changePosition();
					$(e.target).css('background-color','#ffffff');
					impl.processDom();
				}
			});
		},
		changePosition:function(){
			if(this.target != this.src){
				var temp = this.siblingDom[this.src];
				//元素前移
				if(this.target < this.src){
					for(var k=this.src; k > this.target; k--){
						this.siblingDom[k] = this.siblingDom[k-1];
					}
				}
				//元素后移
				if(this.target > this.src){
					for(var p=this.src; p < this.target; p++){
						this.siblingDom[p] = this.siblingDom[p+1];
					}
				}
				//目标位置替换为当前元素
				this.siblingDom[this.target] = temp;
			}
			//刷新元素的新位置
			$(this.parentDom).children('.' + this.className).each(function(i){
				$(this).remove();
			});
			var domLength = this.siblingDom.length;
			for(var j = 0; j < domLength; j++){
				$(this.parentDom).append(this.siblingDom[j].html);
			}
		},
		processDom:function(){
			var parent = document.getElementById(this.parentId);
			this.parentHeight = parent.offsetWidth;
			this.parentWidth = parent.offsetHeight;
			this.parentLeft = this.getX(parent);
			this.parentTop = this.getY(parent);
			this.siblingDom = new Array();
			var dragImpl = this;
			$(this.parentDom).children('.' + this.className).each(function(i){
				dragImpl.saveDom(this,dragImpl.siblingDom);
			});
			this.drag();
		},
		setSrc:function(e){
			var domLength = this.siblingDom.length;
			for(var j = 0; j < domLength; j++){
				if(this.isAtDom(e.clientX,e.clientY,this.siblingDom[j])){
					this.src = j;
					return;
				}
			}
		},
		saveDom:function(e,array){
			var offLeft = e.offsetLeft;
			var offTop = e.offsetTop;
			var left = this.getX(e);
			var top = this.getY(e);
			var right = left + e.offsetWidth;
			var bottom = top + e.offsetHeight;
			array.push({color:e.style.backgroundColor,offLeft:offLeft,offTop:offTop,left:left,top:top,right:right,bottom:bottom,impl:e,html:$("<div></div>").append($(e).clone()).html()});
		},
		isAtDom:function(x,y,dom){
			var scrollLeft = $(document).scrollLeft();
			var scrollTop = $(document).scrollTop();
			if(x > dom.left - scrollLeft && x < dom.right - scrollLeft && y > dom.top - scrollTop && y < dom.bottom - scrollTop){
				return true;
			}
			return false;
		},
		getDom:function(e){
			var domLength = this.siblingDom.length;
			for(var i=0;i<domLength;i++){
				var dom = this.siblingDom[i];
				if(dom.impl == e){
					return dom;
				}
			}
		},
		getX:function(e){
			var x = 0;
			while (e) {
				x += e.offsetLeft;
				e = e.offsetParent;
			}
			return x;
		},
		getY:function(e){
			var y = 0;
			while (e) {
				y += e.offsetTop;
				e = e.offsetParent;
			}
			return y;
		},
		isNotBlank:function(str){
			if(str && str!= null && str!='null' && /\S+/.test(str)){
				return true;
			}
			return false;
		},
		isArray:function(obj){
			return Object.prototype.toString.call(obj) === '[object Array]';
		}
}